package com.itcam.json;

import com.itcam.pojo.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Author:Cammy.Wu
 * Date:2024-04-30
 * Description:
 */


//@Controller
//@ResponseBody
@RequestMapping("json")
@RestController // == @Controller + @ResponseBody
public class JsonController {

    @GetMapping("data1")
    @ResponseBody
    public User data1(){
        // 对象 -> json -> {}
        // 集合 -> json -> []
        User user = new User();
        user.setName("happy doggy");
        user.setAge(5);
        return user;
        // TODO:user -> handlerAdapter -> json -> @ResponseBody -> json直接返回【前后端分离模式】
    }


    /**
     * TODO:@ResponseBody数据直接放入响应体返回不会走视图解析器
     *      快速查找视图，转发和重定向都不生效。。。
     *
     */

    @GetMapping("data2")
    @ResponseBody
    public List<User> data2(){
        User user = new User();user.setName("three doggy");
        user.setAge(12);

        List<User> users = new ArrayList<>();
        users.add(user);
        return users;
    }
}
